package org.ciallo.simplemusicplayer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import org.ciallo.simplemusicplayer.R;
import org.ciallo.simplemusicplayer.adapter.SongAdapter;
import org.ciallo.simplemusicplayer.data.model.Song;
import org.ciallo.simplemusicplayer.dialog.SongOptionsDialogFragment;
import org.ciallo.simplemusicplayer.viewmodel.LocalMusicViewModel;
import org.ciallo.simplemusicplayer.viewmodel.SharedViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jukomu
 * @Description: 示特定文件夹内所有歌曲的 Activity
 * @Package: org.ciallo.simplemusicplayer.activity
 * @Project: SimpleMusicPlayer
 * @name: FolderSongsActivity
 * @Date: 2025/6/3-01:21
 * @Filename: FolderSongsActivity
 */
public class FolderSongsActivity extends AppCompatActivity {

    public static final String EXTRA_FOLDER_PATH = "extra_folder_path";
    public static final String EXTRA_FOLDER_NAME = "extra_folder_name";

    private String folderPath;
    private String folderName;

    private Toolbar toolbar;
    private RecyclerView recyclerViewFolderSongs;
    private SongAdapter songAdapter;
    private TextView tvEmptySongsInFolder;

    private LocalMusicViewModel localMusicViewModel;
    private SharedViewModel sharedViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_folder_songs);

        Intent intent = getIntent();
        folderPath = intent.getStringExtra(EXTRA_FOLDER_PATH);
        folderName = intent.getStringExtra(EXTRA_FOLDER_NAME);

        if (folderPath == null) {
            Toast.makeText(this, "无效的文件夹路径", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        toolbar = findViewById(R.id.toolbar_folder_songs_activity);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(folderName != null ? folderName : "文件夹歌曲");
        }

        recyclerViewFolderSongs = findViewById(R.id.recyclerView_folder_songs_list);
        tvEmptySongsInFolder = findViewById(R.id.textView_empty_songs_in_folder);

        localMusicViewModel = new ViewModelProvider(this).get(LocalMusicViewModel.class);
        sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);

        setupRecyclerView();
        observeViewModel();
    }

    private void setupRecyclerView() {
        songAdapter = new SongAdapter(
                (song, position) -> { // OnSongClickListener
                    List<Song> currentList = songAdapter.getCurrentList();
                    if (sharedViewModel != null && currentList != null && !currentList.isEmpty()) {
                        sharedViewModel.playSongAtIndex(new ArrayList<>(currentList), position);
                    }
                },
                (song, anchorView) -> { // OnSongOptionsClickListener
                    SongOptionsDialogFragment.newInstance(song.getId())
                            .show(getSupportFragmentManager(), SongOptionsDialogFragment.TAG);
                }
        );
        recyclerViewFolderSongs.setLayoutManager(new LinearLayoutManager(this));
        recyclerViewFolderSongs.setAdapter(songAdapter);
    }

    private void observeViewModel() {
        if (folderPath != null) {
            localMusicViewModel.getSongsInFolder(folderPath).observe(this, songs -> {
                if (songs != null && !songs.isEmpty()) {
                    songAdapter.submitList(songs);
                    tvEmptySongsInFolder.setVisibility(View.GONE);
                    recyclerViewFolderSongs.setVisibility(View.VISIBLE);
                } else {
                    songAdapter.submitList(new ArrayList<>());
                    tvEmptySongsInFolder.setVisibility(View.VISIBLE);
                    recyclerViewFolderSongs.setVisibility(View.GONE);
                }
            });
        } else {
            tvEmptySongsInFolder.setVisibility(View.VISIBLE);
            recyclerViewFolderSongs.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
